#ifndef __AKY_BAT_H__
#define __AKY_BAT_H__
#include "aky_program/aky_define.h"

#define CHRG_PORT GPIOA
#define CHRG_PIN PIN13
#if 00
#define READ_CHRG    0//GPIO_GetBit(CHRG_PORT, CHRG_PIN)

#else
#define READ_CHRG    GPIO_GetBit(CHRG_PORT, CHRG_PIN)

#endif
#define CHRG_PIN_INIT  GPIO_Init(CHRG_PORT, CHRG_PIN, 0, 1, 0, 0)
#if (DEBUG_SWITCH_VOICE_METHOD == 1)
#define FULL_PORT GPIOC
#define FULL_PIN PIN4

#else
#define FULL_PORT GPIOA
#define FULL_PIN PIN12

#endif
#define READ_FULL    GPIO_GetBit(FULL_PORT, FULL_PIN)
#define FULL_PIN_INIT  GPIO_Init(FULL_PORT, FULL_PIN, 0, 1, 0, 0)

#define GREEN_PORT GPIOD
#define GREEN_PIN PIN13
#define GREEN_PIN_INIT  GPIO_Init(GREEN_PORT, GREEN_PIN, 1, 0, 0, 0)
#define GREEN_ON GPIO_ClrBit(GREEN_PORT,GREEN_PIN)
#define GREEN_OFF GPIO_SetBit(GREEN_PORT,GREEN_PIN)

#define RED_PORT GPIOD
#define RED_PIN PIN12
#define RED_PIN_INIT  GPIO_Init(RED_PORT, RED_PIN, 1, 0, 0, 0)
#define RED_ON GPIO_ClrBit(RED_PORT,RED_PIN)
#define RED_OFF GPIO_SetBit(RED_PORT,RED_PIN)


#define R4  1000     //1m
#define R5  300     //499k
#define SARADC_FULLRANGE  4096
#define SARADC_REFV     330  //x.xV*100
#define V_OFFSET  26 //补偿

#define CHECK_LIMIT (10ul) //最小检测次数  即取平均次数

#define LOW_BAT_THRESHOLD   (630)
typedef enum
{
    NO_CHARGE = 0,//没有充电 空闲
    CHARGING,//正在充电
    FULL_BAT,// 充满
    NO_BAT_AND_USB_INPUT,//没有锂电但usb供电插入, 此时charge pin会一直翻转(2ms周期)
}e_charge_stat_t;
typedef enum 
{
    BAT_STOP = 0,
    BAT_START,
    BAT_NOT_DISP,
    BAT_DISP,
    BAT_FLASH_DISP,//低电压时闪烁
}e_bat_control_t;

typedef struct 
{
    uint8_t flag_start : 1;
    uint8_t flag_not_disp : 1;
    uint8_t flag_flash_disp : 1;
    uint8_t flag_only_up_once : 1;
    uint8_t flag_full_before : 1;
    uint8_t flag_bat_flash_disp : 1;//闪电图标显示控制
    uint16_t cur_bat_val;
    uint16_t cur_bat_percent;
    uint16_t cur_bat_cells;
    uint16_t bat_cells_last;
    uint16_t bat_percent_last;
    uint16_t bat_cells_charge;
    uint16_t bat_percent_disp;

}s_bat_flush_t;
extern s_bat_flush_t g_s_bat_flush;
extern uint8_t g_flag_low_bat;

void aky_lbt_adc_init(void);
void bat_test(void);
void bat_flush_pro(void);
void bat_init(void);
void charge_stat_detect_pro(void);
uint16_t ADCgetavg1(uint16_t adc);
uint16_t ADCgetavg2(uint16_t adc);
uint16_t get_cur_bat_val_by_math(void);
e_charge_stat_t get_cur_charge_stat(void);
uint8_t check_low_bat_pro(uint16_t low_bat_threshold);
uint8_t check_low_bat_trig(uint16_t low_bat_threshold);
void check_low_bat_init(void);
void show_light(uint8_t stat);
void bat_flush_control(e_bat_control_t e_bat_control);
uint8_t get_bat_flush_init_ready(void);

#endif